********************************************************************************************
*This file cleans population from the Census/ACS needed as a denominator for IPEDS outcomes*
********************************************************************************************

foreach year in 1990 2000 2008 2016 {

use if year==`year' using "$raw_data_lmarket/ipums_census.dta", clear

*Drop institutional group quarters* 
quietly: drop if gqtyped>=100 & gqtyped<=499
*Drop alaska and hawai*
quietly: drop if statefip==2 | statefip==15

keep if age>=18&age<=64

keep cntygp* puma statefip perwt multyear empstat classwkrd ind1990 age educd school relate sploc pernum race hispan sex bpl

*population
gen ipums_pop = 1

*age
foreach var of varlist ipums_pop* {
	gen `var'_a18_24 = (`var'==1 & age>=18&age<=24)
	gen `var'_a19_24 = (`var'==1 & age>=19&age<=24)
	gen `var'_a20_24 = (`var'==1 & age>=20&age<=24)
	gen `var'_a21_24 = (`var'==1 & age>=21&age<=24)
	gen `var'_a21_29 = (`var'==1 & age>=21&age<=29)
	gen `var'_a22_29 = (`var'==1 & age>=22&age<=29)
	gen `var'_a23_29 = (`var'==1 & age>=23&age<=29)
	gen `var'_a24_29 = (`var'==1 & age>=24&age<=29)
	gen `var'_a25_29 = (`var'==1 & age>=25&age<=29)
	gen `var'_a19_34 = (`var'==1 & age>=19&age<=34)
	gen `var'_a20_34 = (`var'==1 & age>=20&age<=34)
	gen `var'_a21_34 = (`var'==1 & age>=21&age<=34)
	gen `var'_a22_34 = (`var'==1 & age>=22&age<=34)
	gen `var'_a23_34 = (`var'==1 & age>=23&age<=34)
	gen `var'_a24_34 = (`var'==1 & age>=24&age<=34)
	gen `var'_a25_34 = (`var'==1 & age>=25&age<=34)
	gen `var'_a35_49 = (`var'==1 & age>=35&age<=49)
	gen `var'_a50_64 = (`var'==1 & age>=50&age<=64)
}

keep cntygp* puma statefip ipums_* perwt multyear

**Merge czones using geography xwalk**
if `year'==1990{
gen puma1990=statefip*10000+puma
collapse (sum) ipums_* [fw=perwt], by(puma1990)  fast
count if puma1990!=.
joinby puma1990 using "$project/xwalks/xwalks_geography/puma1990_czone.dta", unmatched(master)
assert czone!=. 
}
else if `year'==2000  | `year'==2008 {
replace puma=1801 if puma==77777
gen puma2000=statefip*10000+puma
collapse (sum) ipums_* [fw=perwt], by(puma2000)  fast
count if puma2000!=.
joinby puma2000 using "$project/xwalks/xwalks_geography/puma2000_czone.dta", unmatched(master)
assert czone!=. 
}
else if `year'==2016  {
replace puma=1801 if puma==77777
gen puma2010=statefip*100000+puma
collapse (sum) ipums_* [fw=perwt], by(puma2010)  fast
count if puma2010!=.
joinby puma2010 using "$project/xwalks/xwalks_geography/puma2010_czone.dta", unmatched(master)
assert czone!=. 
}


**Aggregate at the czone level**
collapse (sum) ipums_* [iw=afac], by(czone) fast

gen year = `year'

save "$clean_data_lmarket/czone`year'_population.dta", replace
}

*Append files
use "$clean_data_lmarket/czone1990_population.dta", clear
append using "$clean_data_lmarket/czone2000_population.dta"
append using "$clean_data_lmarket/czone2008_population.dta"
append using "$clean_data_lmarket/czone2016_population.dta"

gen yearGroup = 1994 if year == 1990
replace yearGroup = 2000 if year == 2000
replace yearGroup = 2007 if year == 2008
replace yearGroup = 2014 if year == 2016

drop year

save "$clean_data_lmarket/census_PopulationDataCZ.dta", replace








